#include "ConsoleWindow.h"

/// <Summary> The instance of this class </Summary>
ConsoleWindow* ConsoleWindow::instance = NULL;

////////////////////////////////////////////////////////////////////////////////////////////////////
/// <Summary>	Gets the instance of this class </Summary>
///
/// <Return>	The instance of this class </Return>
////////////////////////////////////////////////////////////////////////////////////////////////////
ConsoleWindow& ConsoleWindow::GetInstance()
{
	if(NULL == instance)
	{
		instance = new ConsoleWindow;
		instance->Create();
	}
	return *instance;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/// <Summary>	Deletes the instance of this class </Summary>
////////////////////////////////////////////////////////////////////////////////////////////////////
void ConsoleWindow::DeleteInstance()
{
	SAFE_DELETE(instance);
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/// <Summary>	Creates the console </Summary>
////////////////////////////////////////////////////////////////////////////////////////////////////
void ConsoleWindow::Create()
{
	AllocConsole();

	outputHandle = GetStdHandle(STD_OUTPUT_HANDLE);

	inputHandle = GetStdHandle(STD_INPUT_HANDLE);
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/// <Summary>	Writes out a string to the console </Summary>
///
/// <Param name="out">   	The console window </Param>
/// <Param name="string">	The string </Param>
///
/// <Return>	This console window </Return>
////////////////////////////////////////////////////////////////////////////////////////////////////
ConsoleWindow& operator<<(ConsoleWindow& out, const char* string)
{
	DWORD written;
	WriteConsole(out.outputHandle, string, strlen(string), &written, NULL);

	return out;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/// <Summary>	Writes out a letter to the console </Summary>
///
/// <Param name="out">   	The console window </Param>
/// <Param name="letter">	The letter </Param>
///
/// <Return>	This console window </Return>
////////////////////////////////////////////////////////////////////////////////////////////////////
ConsoleWindow& operator<<(ConsoleWindow& out, char letter)
{
	out << &letter;

	return out;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/// <Summary>	Writes out a float to the console </Summary>
///
/// <Param name="out">   	The console window </Param>
/// <Param name="value">	The value </Param>
///
/// <Return>	This console window </Return>
////////////////////////////////////////////////////////////////////////////////////////////////////
ConsoleWindow& operator<<(ConsoleWindow& out, float value)
{
	char buffer[ConsoleWindow::MAX_STRING_LENGTH];

	sprintf_s(buffer, ConsoleWindow::MAX_STRING_LENGTH, "%f", value);

	out << buffer;

	return out;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/// <Summary>	Writes out a double to the console </Summary>
///
/// <Param name="out">   	The console window </Param>
/// <Param name="value">	The value </Param>
///
/// <Return>	This console window </Return>
////////////////////////////////////////////////////////////////////////////////////////////////////
ConsoleWindow& operator<<(ConsoleWindow& out, double value)
{
	char buffer[ConsoleWindow::MAX_STRING_LENGTH];

	sprintf_s(buffer, ConsoleWindow::MAX_STRING_LENGTH, "%f", value);

	out << buffer;

	return out;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/// <Summary>	Writes out a short to the console </Summary>
///
/// <Param name="out">   	The console window </Param>
/// <Param name="value">	The value </Param>
///
/// <Return>	This console window </Return>
////////////////////////////////////////////////////////////////////////////////////////////////////
ConsoleWindow& operator<<(ConsoleWindow& out, short value)
{
	char buffer[ConsoleWindow::MAX_STRING_LENGTH];

	sprintf_s(buffer, ConsoleWindow::MAX_STRING_LENGTH, "%d", value);

	out << buffer;

	return out;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/// <Summary>	Writes out a short to the console </Summary>
///
/// <Param name="out">   	The console window </Param>
/// <Param name="value">	The value </Param>
///
/// <Return>	This console window </Return>
////////////////////////////////////////////////////////////////////////////////////////////////////
ConsoleWindow& operator<<(ConsoleWindow& out, unsigned short value)
{
	char buffer[ConsoleWindow::MAX_STRING_LENGTH];

	sprintf_s(buffer, ConsoleWindow::MAX_STRING_LENGTH, "%d", value);

	out << buffer;

	return out;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/// <Summary>	Writes out a int to the console </Summary>
///
/// <Param name="out">   	The console window </Param>
/// <Param name="value">	The value </Param>
///
/// <Return>	This console window </Return>
////////////////////////////////////////////////////////////////////////////////////////////////////
ConsoleWindow& operator<<(ConsoleWindow& out, int value)
{
	char buffer[ConsoleWindow::MAX_STRING_LENGTH];

	sprintf_s(buffer, ConsoleWindow::MAX_STRING_LENGTH, "%d", value);

	out << buffer;

	return out;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/// <Summary>	Writes out a int to the console </Summary>
///
/// <Param name="out">   	The console window </Param>
/// <Param name="value">	The value </Param>
///
/// <Return>	This console window </Return>
////////////////////////////////////////////////////////////////////////////////////////////////////
ConsoleWindow& operator<<(ConsoleWindow& out, unsigned int value)
{
	char buffer[ConsoleWindow::MAX_STRING_LENGTH];

	sprintf_s(buffer, ConsoleWindow::MAX_STRING_LENGTH, "%d", value);

	out << buffer;

	return out;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/// <Summary>	Writes out a long to the console </Summary>
///
/// <Param name="out">   	The console window </Param>
/// <Param name="value">	The value </Param>
///
/// <Return>	This console window </Return>
////////////////////////////////////////////////////////////////////////////////////////////////////
ConsoleWindow& operator<<(ConsoleWindow& out, long value)
{
	char buffer[ConsoleWindow::MAX_STRING_LENGTH];

	sprintf_s(buffer, ConsoleWindow::MAX_STRING_LENGTH, "%d", value);

	out << buffer;

	return out;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
/// <Summary>	Writes out a long to the console </Summary>
///
/// <Param name="out">   	The console window </Param>
/// <Param name="value">	The value </Param>
///
/// <Return>	This console window </Return>
////////////////////////////////////////////////////////////////////////////////////////////////////
ConsoleWindow& operator<<(ConsoleWindow& out, unsigned long value)
{
	char buffer[ConsoleWindow::MAX_STRING_LENGTH];

	sprintf_s(buffer, ConsoleWindow::MAX_STRING_LENGTH, "%d", value);

	out << buffer;

	return out;
}